clear all

cd "~/Dropbox (Harvard University)/Monetary Policy with Heterogenous Producers/Replication Code"

ssc install grstyle, replace
grstyle init
grstyle set plain, nogrid
graph set window fontface "Palatino"

******* Assemble dataset

import excel "data/markup_estimates/FRED/quarterly_tfp.xlsx", sheet("quarterly") firstrow clear
rename date quarter
keep quarter dY dhours dLQ dk dtfp dtfp_util alpha
drop if quarter==""
gen year = substr(quarter, 1, 4)
destring year, replace
save "data/markup_estimates/FRED/quarterly_tfp_clean", replace

merge m:1 year using "data/markup_estimates/aggregate_markup_mu_li_annual"
drop _merge
gen labor_rev_share = 1 - alpha
gen labor_cost_share = mu_bar * labor_rev_share
gen dtfp_rep = dY - (1-alpha)*(dLQ+dhours) - alpha*dk
gen dtfp_labor = dY - (dLQ+dhours)
gen dtfp_distadj = dY - labor_cost_share*(dLQ+dhours) - (1-labor_cost_share)*dk

gen date = quarterly(quarter, "YQ")
format date %tq
drop quarter
g quarter = date 

merge m:1 year quarter using "data/data_temp/quarterly_aggregates_clean"
drop _merge
rename resid_full resid_full2

merge 1:1 date using "data/Monetary_shocks/RR_monetary_shock_quarterly"
drop _merge

tsset quarter

drop if year >= 2008

******* Local projections of monetary shocks on aggregate variables

local hmax = 16
local cycle_var resid_full
local tfp_list dtfp_labor dtfp_rep dtfp_distadj dY 
foreach tfp_var in `tfp_list' {
// 	forvalues h = 0/`hmax' {
// 		qui regress F`h'.`tfp_var' L(0/4).`cycle_var' L(1/4).`tfp_var', vce(robust)
// 		eststo p`h' 
// 	}
// 	*coefplot (p*), vertical keep(`cycle_var') asequation swapnames eqrename(^p(.*)$ = \1, regex) color("31 119 180") yline(0) legend(off) xtitle("Quarters after monetary shock") ytitle("Change in TFP (%)") recast(connected) levels(68 95) ciopts(recast(rarea rconnected) color(%20 gray%50) lwidth(0) msymbol(i i) lpattern(dash dash)) ysize(1) xsize(2)
// 	*gr rename fig_diff_`tfp_var', replace
// 	eststo clear
	
	gen cum_`tfp_var' = sum(`tfp_var')
	replace cum_`tfp_var'=. if `tfp_var'==.
	forvalues h = 0/`hmax' {
		gen cum`h'_`tfp_var' = (F`h'.cum_`tfp_var' - L1.cum_`tfp_var')/4
	}
	forvalues h = 0/`hmax' {
		qui newey cum`h'_`tfp_var' L(0/4).`cycle_var' L(1/4).`tfp_var', lag(4)
		eststo p`h' 
		disp _b[`cycle_var']
	}
	local measure_name = cond("`tfp_var'" == "dtfp_labor", "labor prod.", cond("`tfp_var'" == "dY", "output", "TFP"))
	if "`tfp_var'"=="dY" {
		coefplot (p*), vertical keep(`cycle_var') asequation swapnames eqrename(^p(.*)$ = \1, regex) color("31 119 180") yline(0) legend(off) xtitle("Quarters since monetary shock") ytitle("Change in `measure_name' (%)") recast(connected) msymbol(o) levels(68 95) ciopts(recast(rarea rconnected) color(%20 gray%50) lwidth(0) msymbol(i i) lpattern(dash dash)) ysize(3) xsize(6) scale(1.7) 
	} 
	else {
		coefplot (p*), vertical keep(`cycle_var') asequation swapnames eqrename(^p(.*)$ = \1, regex) color("31 119 180") yline(0) legend(off) xtitle("Quarters since monetary shock") ytitle("Change in `measure_name' (%)") recast(connected) msymbol(o) levels(68 95) ciopts(recast(rarea rconnected) color(%20 gray%50) lwidth(0) msymbol(i i) lpattern(dash dash)) ysize(3) xsize(6) scale(1.7) ytick(-2(1)1) ylabel(-2(1)1)
	}
	gr rename fig_cum_`tfp_var', replace
	graph export "outputs/TFP_LP_`tfp_var'_`cycle_var'.pdf", replace fontface("Palatino")
	eststo clear
}
drop cum*
